package com.lingx.service.query;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.lingx.model.IContext;
import com.lingx.model.IPerformer;
import com.lingx.model.impl.DefaultField;
import com.lingx.utils.SQLUtils;
import com.lingx.utils.Utils;
/**
 * lingxparams开启参数过滤，例如：
ParamsCondition: and type='3' 
 * @author lingx
 *
 */
@Component
public class ParamsCondition implements IGridCondition{

	@Override
	public String getCondition(Map<String, Object> params, IContext context, IPerformer performer) {
		StringBuilder sb=new StringBuilder();
		if(params.containsKey("lingxparams")) {
			String temp;
			Map<String,String> map=new HashMap<>();
			List<DefaultField> list=context.getEntity().getFields().getList();//Grid的fields不能用。线程不安全
			for(DefaultField field:list) {
				if(params.containsKey(field.getCode())) {
					temp=SQLUtils.getValue(params.get(field.getCode()));
					if(Utils.isNotNull(temp))
					map.put(field.getCode(),temp);
				}
			}
			if(!map.isEmpty()) {
				for(String key:map.keySet()) {
					sb.append(" and ").append(key).append("='").append(map.get(key)).append("' ");
				}
			}
			//System.out.println("ParamsCondition:"+sb.toString());
		}
		return sb.toString();
	}

}
